package com.chenglai.goods.service;

import com.chenglai.goods.dao.GoodsAttrGroupMapper;
import com.chenglai.goods.dao.GoodsAttrMapper;
import com.chenglai.goods.dao.GoodsInventoryMapper;
import com.chenglai.goods.dao.GoodsMapper;
import com.chenglai.goods.pojo.Goods;
import com.chenglai.goods.pojo.GoodsAttr;
import com.chenglai.goods.pojo.GoodsAttrGroup;
import com.chenglai.goods.pojo.GoodsInventory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yawang
 * @create 2023/11/16 14:22:12
 **/
@Service
public class GoodsServiceImpl implements GoodsService{
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsAttrGroupMapper attrGroupMapper;
    @Autowired
    private GoodsAttrMapper attrMapper;
    @Autowired
    private GoodsInventoryMapper inventoryMapper;
    @Override
    public PageInfo listGoods(Integer shopId, Integer page, Integer size, Integer categoryId, Integer flag, String startTime, String endTime, String search) {
        PageHelper.startPage(page,size);
        return new PageInfo(goodsMapper.listGoods(categoryId,startTime,endTime,shopId,search));
    }

    @Override
    public void addGoods(Goods goods, String flag) {
        if(flag.equals("add")){
            //添加主商品信息
            goodsMapper.insertGoods(goods);
            for (GoodsAttrGroup attrGroup : goods.getGoodsAttrGroupList()) {
                attrGroup.setGoodsId(goods.getGoodsId());
                //添加规格组   例如：颜色，尺码
                attrGroupMapper.addGroup(attrGroup);
                List<GoodsAttr> attrs = new ArrayList<>();
                for (GoodsAttr attr : attrGroup.getGoodsAttrList()) {
                    attr.setGroupId(attrGroup.getAttrGroupId());
                    attrs.add(attr);
                }
                //添加规格组里面的子规格，例如l,xl，xxl，黑色
                attrMapper.insertList(attrs);
                //xinzengkucun
                List<GoodsInventory> inventoryList = new ArrayList<>();
                for (GoodsInventory inventory : goods.getInventoryList()) {
                    inventory.setGoodsId(goods.getGoodsId());
                    inventoryList.add(inventory);
                }
                inventoryMapper.insertList(inventoryList);

            }
        }else{
            //修改商品


        }

    }

    @Override
    public List<Goods> listGroupGoods(Integer shopId) {
        return goodsMapper.listGroupGoods( shopId);
    }
}
